"""
需求：爬取人民邮电出本社的图书信息，保存到mongodb数据库中
算法分析：
        1.逆向分析获取url链接地址
        2.分析url地址，动态改变分页信息
        3.发送请求并服务器响应信息
        4.解析数据，json 模块进行类型转换
        5.结果保存到mongo数据库中
"""
import json
import pymongo
import requests
import time
# 连接mongodb数据库
client = pymongo.MongoClient(host='192.168.231.133', port=27017)
print(client)
# 创建数据库
db = client['db_yd']
# 删除表  实现数据更新
dl = db.tb_yd.drop()
# 创建表
tb = db['tb_yd']
for i in range(10):
    url = f'https://www.ptpress.com.cn/hotBook/getHotBookList?parentTagId=75424c57-6dd7-4d1f-b6b9-8e95773c0593&rows={20}&page={i}'
    response = requests.get(url)
    print(response)
    # 获取字符串类型的响应信息
    datas = response.text
    # 转成json格式的数据
    json_datas = json.loads(datas)
    data = json_datas['data']
    rows = data['rows']

    for row in rows:
        bookName = row['bookName']
        price = row['price']
        picPath = row['picPath']
        # 输出信息
        print(bookName, price, picPath)
        # url = f'{picPath}'
        # client = requests.get(url)
        # content = response.content.decode("UTF8")
        time.sleep(0.1)
        tb.insert_one({'书名': bookName, '定价': price, '封面': picPath})
# 关闭数据库  释放资源
client.close()
